/**
 * @name variable-read search
 * @id python/variable-read-search
 */

import python
import util

predicate allowIterTypeOfStmt(Stmt stmt) {
    stmt instanceof Return or
    stmt instanceof While or
    stmt instanceof ExprStmt or
    stmt instanceof If or
    stmt instanceof Global or
    stmt instanceof Assert or
    stmt instanceof Raise 
}

Expr iterStmtRead(Stmt stmt)
{
    allowIterTypeOfStmt(stmt) and
    if stmt instanceof Return then
        result=stmt.(Return).getValue()
    else if stmt instanceof While then
        result=stmt.(While).getTest()
    else if stmt instanceof ExprStmt then
        result=stmt.(ExprStmt).getValue()
    else if stmt instanceof If then
        result=stmt.(If).getTest()
    else if stmt instanceof Global then
        result=stmt.(Global).getASubExpression()
    // else if stmt instanceof Assert then
    //     result=stmt.(Assert).getTest()
    else
        result=stmt.getASubExpression()
}


from File f,Module mod,Scope scope,string scopeName,Expr exprBody,Expr exprBodyIter,string varName,Location location,string loc,Stmt stmt
where
//debug(location) and
isSource(f) and
scope=stmt.getScope() and
mod=scope.getEnclosingModule() and
f=mod.getFile() and


exprBody=iterStmtRead(stmt) and
exprBodyIter=iterExpr(exprBody) and
varName=expr2string(exprBodyIter) and

//f.getAbsolutePath()="/opt/src/pro/builders/optimizer_builder.py" and
location=exprBodyIter.getLocation() and
loc=getLocText(location) and
scopeName=getScopeName(scope)
select f,mod,scope,varName,stmt,exprBody,scopeName,loc